package com.jytc.framework.bo.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jytc.framework.bo.FwLoginBO;
import com.jytc.framework.bo.FwPermissionBO;
import com.jytc.framework.commons.AdminLoginToken;
import com.jytc.framework.constants.ResultConstants;
import com.jytc.framework.constants.SessionNameConstants;
import com.jytc.framework.dao.FwUserDAO;
import com.jytc.framework.po.FwPermission;
import com.jytc.framework.po.FwRole;
import com.jytc.framework.po.FwRolePermission;
import com.jytc.framework.po.FwUser;
import com.jytc.framework.po.FwUserRole;
import com.jytc.framework.utils.SessionUtils;

/**
 * @title: 
 * @description: 
 * 
 * @author: dongyuese
 *
 */
@Service
public class FwLoginBOImpl implements FwLoginBO
{
	/**
	 * 用户登录信息数据访问对象
	 */
	@Autowired
	private FwUserDAO fwUserDAO;

	@Autowired
	private FwPermissionBO wPermissionBO;

	@Transactional(readOnly = false, rollbackFor = Throwable.class)
	public int login(String userName, String loginPwd, String checkCode, HttpServletRequest request) throws Exception
	{
		String loginIp = request.getRemoteAddr();
		int loginResultCode;

		// 检查验证码是否正确
		if (!SessionUtils.checkImgCheckCode(request, checkCode))// 验证失败
		{
			loginResultCode = ResultConstants.LOGIN_CODE_FAILED;
			return loginResultCode;
		}

		// 检查登录名和密码是否正确
		FwUser fwUser = fwUserDAO.findUnique("userName", userName);

		if (fwUser == null || !fwUser.getPassword().equals(loginPwd))
		{
			loginResultCode = ResultConstants.LOGIN_INFO_FAILED;
			return loginResultCode;
		}

		// 登录成功
		loginResultCode = ResultConstants.LOGIN_SUCCESS;

		// 加载登录用户的相关信息到登录令牌
		AdminLoginToken loginToken = getAdminLoginToken(fwUser);
		// 保存登录用户信息到http session
		SessionUtils.setObjectAttribute(request, SessionNameConstants.ADMIN_LOGIN_TOKEN, loginToken);

		// 修改最后登录时间
		//fwUser.setLoginLastTime(DateTimeUtils.getChar14());
		//fwUserDAO.update(fwUser);

		return loginResultCode;

	}

	/**
	 * 加载登录用户的相关信息到登录令牌
	 * 
	 * @param frameSysLogin
	 * @return
	 */
	private AdminLoginToken getAdminLoginToken(FwUser fwUser) throws Exception
	{
		AdminLoginToken loginToken = new AdminLoginToken();
		loginToken.setFwUser(fwUser);

		// 设置登录信息
		List<FwRole> fwRoles = new ArrayList<FwRole>();
		Map<Integer, String> menuPermissions = new HashMap<Integer, String>();
		// 设置用户权限
		if (fwUser.getFwUserRoles() != null)
		{
			//如果是超级管理员用户，则显示所有菜单

			if (fwUser.getUserType() == 0)
			{
				List<FwPermission> permissions = wPermissionBO.findBySystemId(fwUser.getSystemType());
				for (FwPermission sysPermission : permissions)
				{
					menuPermissions.put(sysPermission.getPermissionId(), sysPermission.getPermissionName());
				}
			}
			//如果不是超级管理员，则根据分配的角色进行权限查询
			else
			{
				for (FwUserRole fwLoginRole : fwUser.getFwUserRoles())
				{
					FwRole fwRole = fwLoginRole.getFwRole();
					fwRoles.add(fwRole);

					for (FwRolePermission rolePermission : fwRole.getFwRolePermissions())
					{
						menuPermissions.put(rolePermission.getFwPermission().getPermissionId(), rolePermission
								.getFwPermission().getPermissionName());
					}
				}
			}
		}
		loginToken.setFwRoles(fwRoles);
		loginToken.setMenuPermissions(menuPermissions);
		loginToken.setAllMenus(wPermissionBO.findAll());
		return loginToken;
	}

	/**
	 * 根据登录名，密码和新密码 进行密码修改
	 * @param loginName 登录名
	 * @param loginPwd 原来的密码
	 * @param newLoginPwd 新密码
	 * @return 成功或者失败
	 * @throws Exception  抛出异常
	 */
	public int updPassword(String loginPwd, String newLoginPwd, HttpServletRequest request) throws Exception
	{
		AdminLoginToken loginToken = (AdminLoginToken) request.getSession().getAttribute(SessionNameConstants.ADMIN_LOGIN_TOKEN);
		if (loginToken.getFwUser().getPassword().equals(loginPwd))
		{
			FwUser user = loginToken.getFwUser();
			user.setPassword(newLoginPwd);
			fwUserDAO.update(user);
			return ResultConstants.UPDATE_PASSWORD_SUCCEED;
		} else
		{
			return ResultConstants.UPDATE_PASSWORD_FAILED;
		}
	}
}
